#include <iostream>
#include <cstring>
#include "Borrow_view_achieve.h"
#include "Tool.h"

Borrow_view_achieve::Borrow_view_achieve(void)
{
	b_service = new Borrow_service_achieve;
}

bool Borrow_view_achieve::login(void)
{
	int id;
	for(int i = 0; i < 8;i++)
	{
		puts("");
	}
	cout<<"			   Input id：";
	cin >> id;
	cin.get();
	cout << endl;
	char *passwd=getpass("			   Password: ");
	return b_service->loginsuccess(id,passwd);
}

void Borrow_view_achieve::menu(void)
{
	if(login())
	{
		login_loading();
		while(1)
		{
			int choice = 0;  
		  	do
		  	{  
		  		
			    system("clear");  
			   	puts("	  ╭════════════════════════════════════════════════════╮");  
   				puts("	  │               　 图书馆用户界面　　　   　 　　    │");  
		    	puts("	  ╰════■□■□══════════════════════════════════■□■□══════╯"); 
		   		puts("");  
			    cout << "   	 	   	    1. 查找书籍             		      \n";  
			    cout << "   	 	   	    2. 评价书籍                         \n";   
			    cout << "   	 	   	    3. 显示书目         		          \n";  
			    cout << "	　   	   	    4. 显示书籍                         \n";  
			    cout << "   	 	   	    5. 显示个人信息                     \n";  
			    cout << "   	 	   	    0. 退出                             \n";  
			    puts("");
				puts("	  ╭════════════════════════════════════════════════════╮"); 
				puts("	  ╰════■□■□══════════════════════════════════■□■□══════╯"); 
				puts("");  
			    cout << "	 	   	     请选择：";
			    stdin -> _IO_read_ptr = stdin -> _IO_read_end;  
			    choice = getch();
			}while(choice < '0'||choice > '5');

			switch(choice-'0')
			{
				case 0:return;
				case 1:find();break;
				case 2:revert();break;
				case 3:listclass();break;
				case 4:listbook();break;
				case 5:listmymsg();break;
			}
			anykey2c();
		}
	}
	else
	{
		cout << "密码错误" << endl;
	}
	
}

void Borrow_view_achieve::find(void)
{
	system("clear");
	b_service->topborrowbook();
	b_service->toppraisebook();
	int select;
	cout << endl;
	cout << "请选择输入书号还是书名 (0/1) :";
	cin >> select;cin.get();
	if(1 == select)
	{
		char bookname[20];
		cout << "请输入书名:";
		cin.get(bookname,sizeof(bookname)).get();
		b_service->findbook(bookname);
	}
	else
	{
		int bookid;
		cout << "请输入书号:";
		cin >> bookid;
		cout << endl;
		cin.get();
		b_service->findbook(bookid);
	}
}

void Borrow_view_achieve::revert(void)
{
	system("clear");
	int bookid=0;
	int score=0;
	char comment[49] = {};
	cout << "请输入要评价的书号: ";
	cin >> bookid;
	cin.get();
	
	cout << "请输入对此书的评分(0~5): ";
	do{
		cin >> score;cin.get();
	}while(score<0||score>5);
	
	cout << "是否要对此书评论( y / n): ";
	if(getch() == 'y')
	{
		cout << "\n请输入对此书的评价(限16字): ";
		cin >> comment;
		clear_stdin();
		b_service->scorebook(bookid,score,comment);
		return;
	}
	else
	{
		cout << endl;
		return;
	}	
}

void Borrow_view_achieve::listclass(void)
{
	system("clear");
	loading();
	b_service->listclass();
}

void Borrow_view_achieve::listbook(void)
{
	system("clear");
	int classid;
	cout << "请输入要显示的书目id:";
	cin >> classid;cin.get();
	b_service->listbook(classid);
}

void Borrow_view_achieve::listmymsg(void)
{
	system("clear");
	b_service->listmymsg();
}

Borrow_view_achieve::~Borrow_view_achieve(void)
{
	delete b_service;
}
